package croppingplanap;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.FileWriter;

import javax.swing.BorderFactory;
import javax.swing.JApplet;
import javax.swing.JPanel;
import java.net.*;

public class CroppingPlanAp extends JApplet{

	private RectSquarePanel rectSquarePanel;
	private CroppingPlan plan;	
	private LegendPanel legend;
	
	private JPanel mainPanel;
	
	boolean ready = false;
	
	public void start(){
		mainPanel = new JPanel();
		
		String catStr = this.getParameter("all_categories");
		String cropStr = this.getParameter("all_crops");
		String mapStr = this.getParameter("map_to_load");
		String firstCropStr = this.getParameter("first_crop");
			
		if(catStr == null || cropStr == null || mapStr == null){
			//plan = CroppingPlan.unserialize("10;10;******____________01******01____________******______________******__________________________********____________********____________********____________********____________********____________********");
			StringBuilder writer = new StringBuilder();
			
			writer.append("100;55;");
			for(int y = 0; y < 25; y++){
				for(int x = 0; x < 100; x++){
					writer.append("__");
				}
			}
			for(int y = 25; y < 55; y++){
				for(int x = 0; x < 62; x++){
					writer.append("__");
				}
				for(int x = 62; x < 100; x++){
					writer.append("**");
				}
			}
			
			
			plan = CroppingPlan.unserialize(writer.toString());
		}else{
			plan = CroppingPlan.unserialize(catStr, cropStr, mapStr);
		}
		this.setLayout(new BorderLayout());
				
		legend = new LegendPanel(this, plan);
		rectSquarePanel = new RectSquarePanel(plan, legend);
		mainPanel.add(rectSquarePanel);
		mainPanel.add(legend);
		mainPanel.setBackground(Color.blue);
		mainPanel.setBorder(BorderFactory.createLineBorder(Color.blue));
		rectSquarePanel.setReady(true);
		
		if(firstCropStr != null){
			setCropFromJavascript(firstCropStr);
		}else if(plan.getCrops().size() == 0){
			throw new RuntimeException("No crops exist");
		}else{
			Crop firstCrop = plan.getCrops().values().iterator().next();
			rectSquarePanel.setCurrentCrop(firstCrop);
			legend.setCrop(firstCrop);
		}
		
		this.add(mainPanel, BorderLayout.CENTER);
		
		ready = true;
		
		legend.setReady();
		legend.refreshLegend();
		this.validate();
		this.repaint();
	}
	
	public void setCropFromJava(Crop nextCrop){
		rectSquarePanel.setCurrentCrop(nextCrop);
		legend.setCrop(nextCrop);
	    try {
	      getAppletContext().showDocument
	        (new URL("javascript:setCropFromJava(\"" + nextCrop.getId() +"\")"));
	      }
	    catch (MalformedURLException me) { }

	}
	
	public void setCropFromJavascript(String idStr){
		int id;
		try{
			id = Integer.parseInt(idStr);
		}catch(NumberFormatException e){
			throw new RuntimeException("Crop id \"" + idStr + "\" is not a number");
		}
		Crop nextCrop = plan.getCrops().get(id);
		if(nextCrop == null){
			throw new RuntimeException("Crop with id " + id + " does not exist");
		}
		rectSquarePanel.setCurrentCrop(nextCrop);
		legend.setCrop(nextCrop);
	}
	
}
